1114E - Arithmetic Progression - CodeForces Solution


binary search interactive number theory probabilities *2200

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <random>
//#pragma GCC optimize ("Ofast, no-stack-protector, unroll-loops, fast-math, O3")
using namespace std;
using ll = long long;
using ld = long double;
#define el '\n'
#define mp(x, y) make_pair(x,y)
#define popcount __builtin_popcountll
#define ios ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
const int mod = 998244353;
const int INF = 1e7;
const ld pi = acos(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll rand(ll l, ll r) {
    return uniform_int_distribution<ll>(l, r)(rng);
}

int main() {
    int n;
    cin >> n;
    int l = 0, r = 1e9, cnt = 60;
    while(l <= r){
        int mid = (l + r) >> 1;
        cnt--;
        cout << "> " << mid << endl;
        int num;cin >> num;
        if(num){
            l = mid + 1;
        }else{
            r = mid - 1;
        }
    }
    int mx = l;
    vector<int> arr;
    set<int>s;
    while(cnt-- && s.size() < n){
        int idx = rand(1, n);
        while(s.count(idx))
            idx = rand(1, n);
        s.insert(idx);
        cout << "? " << idx << endl;
        int num;
        cin >> num;
        arr.push_back(num);
    }
    sort(arr.begin(), arr.end());
    arr.push_back(mx);
    int g = 0;
    for(int i = 0;i < arr.size() - 1;i++){
        for(int j = i + 1;j < arr.size();j++){
            g = __gcd(arr[j] - arr[i], g);
        }
    }
    int start = mx - g * (n - 1);
    cout << "! " << start << " " << g << endl;
}


Comments

Submit
0 Comments
More Questions

137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence